package logic

import (
	"context"
	"fmt"
	"sz2/C/rpc/model/mysql"

	"sz2/C/rpc/good"
	"sz2/C/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListLogic {
	return &ListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListLogic) List(in *good.ListRequest) (*good.ListResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Goods{}
	list, err := g.List(int(in.Page), int(in.PageSize))
	if err != nil {
		return nil, fmt.Errorf("查询失败")
	}
	fmt.Println(list)
	var lists []*good.ListItem
	for _, item := range list {
		lists = append(lists, &good.ListItem{
			UserId:     int64(item.UserId),
			GoodsTitle: item.GoodsTitle,
			GoodsPrice: float32(item.GoodsPrice),
			GoodsCover: item.GoodsCover,
			GoodsStock: int64(item.GoodsStock),
			LikeCount:  int64(item.LikeCount),
			GoodId:     int64(item.ID),
		})
	}
	fmt.Println(lists)
	return &good.ListResponse{List: lists}, nil
}
